package com.example.zjx8969.myapplication.view

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable
import android.text.TextUtils
import android.util.AttributeSet
import android.util.Log
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.appcompat.widget.SwitchCompat
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.content.ContextCompat
import com.example.zjx8969.myapplication.DensityUtil
import com.example.zjx8969.myapplication.R

/**
 * 等效于[R.layout.view_item_setting]
 */
class SettingItemView @JvmOverloads constructor(context: Context, attr: AttributeSet? = null, def: Int = 0)
    : ConstraintLayout(context, attr, def) {
    private var viewIndex: Int = 0
    var titleTextView: AutofitTextView
    var descriptionTextView: AppCompatTextView
    var arrowImageView: ImageView? = null
    var toggleButton: SwitchCompat? = null
    val constraintSet = ConstraintSet()

    init {
        val type = context.obtainStyledAttributes(attr, R.styleable.SettingItemView)
        val titleString = type.getString(R.styleable.SettingItemView_title)
        val arrowVisible = type.getBoolean(R.styleable.SettingItemView_hasRightArrow, true)
        val switchVisible = type.getBoolean(R.styleable.SettingItemView_hasRightSwitch, false)
        type.recycle()
//        Log.e("tag", "switchVisible=$switchVisible,arrowVisible=$arrowVisible,title=$titleString")
        val padding = DensityUtil.dip2px(context, 5f)
        setPadding(0, padding, 0, padding)
        val dp10 = DensityUtil.dp2px(10f).toInt()
        titleTextView = AutofitTextView(context).apply {
            id = R.id.settingItemTitle
            gravity = Gravity.CENTER_VERTICAL or Gravity.START
            if (!titleString.isNullOrEmpty()) text = titleString
            setTextColor(ContextCompat.getColor(context, R.color.item_setting_title_color))
            setMaxTextSize(TypedValue.COMPLEX_UNIT_SP, 15f)
            setMinTextSize(TypedValue.COMPLEX_UNIT_SP, 10f)
            maxLines = 1
            ellipsize = TextUtils.TruncateAt.END
            setPadding(dp10, dp10, dp10, dp10)
            maxWidth = DensityUtil.dip2px(context, 320f)
            add2View(this) {
                startToStart = LayoutParams.PARENT_ID
                setMargins(DensityUtil.dip2px(context, 21f), 0, 0, 0)
            }
        }
        if (arrowVisible) arrowImageView = AppCompatImageView(context).apply {
            id = R.id.settingItemArrow
            setImageResource(R.drawable.item_setting_arrow)
            add2View(this) {
                endToEnd = LayoutParams.PARENT_ID
                setMargins(0, 0, DensityUtil.dip2px(context, 20f), 0)
            }
        }

        descriptionTextView = AppCompatTextView(context).apply {
            id = R.id.settingItemDescription
            gravity = Gravity.CENTER_VERTICAL or Gravity.END
            maxLines = 1
            ellipsize = TextUtils.TruncateAt.END
            setTextSize(TypedValue.COMPLEX_UNIT_SP, 15f)
            setTextColor(ContextCompat.getColor(context, R.color.item_setting_nameTxt_color))
            add2View(this, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) {
                val margin = if (arrowVisible || switchVisible) {
                    DensityUtil.dip2px(context, 10f)
                } else {
                    DensityUtil.dip2px(context, 40f)
                }
                setMargins(0, 0, margin, 0)
            }
        }
        val descriptionEndId = if (arrowVisible) R.id.settingItemArrow else R.id.settingItemToggle
        constraintSet.connect(R.id.settingItemDescription, ConstraintSet.START, R.id.settingItemTitle, ConstraintSet.END)
        constraintSet.connect(R.id.settingItemDescription, ConstraintSet.END, descriptionEndId, ConstraintSet.START)
        constraintSet.connect(R.id.settingItemDescription, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP)
        constraintSet.connect(R.id.settingItemDescription, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM)

        constraintSet.applyTo(this)
        if (switchVisible) {
            val toggleWidth = DensityUtil.dip2px(context, 45f)
            toggleButton = SwitchCompat(context).apply {
                id = R.id.settingItemToggle
                gravity = Gravity.END.or(Gravity.CENTER_VERTICAL)
                setThumbResource(android.R.color.transparent)
                setTrackResource(R.drawable.btn_toggle_selector)
                switchMinWidth = toggleWidth
                isChecked = true
                isFocusable = false
                add2View(this, width = toggleWidth, height = DensityUtil.dip2px(context, 27f)) {
                    rightToRight = LayoutParams.PARENT_ID
                    setMargins(0, 0, DensityUtil.dip2px(context, 10f), 0)
                }
            }
        }
        isClickable = true
        background = StateListDrawable().apply {
            addState(intArrayOf(android.R.attr.state_pressed), ColorDrawable(Color.rgb(224, 224, 224)))
            addState(intArrayOf(), ColorDrawable(Color.WHITE))
        }
    }

    @JvmOverloads
    fun setDescriptionTextColor(text: String, iconId: Int = 0, textColor: Int = -1) {
        descriptionTextView.text = text
        if (textColor != -1) descriptionTextView.setTextColor(textColor)
        val drawable: Drawable? = if (iconId != 0) {
            ContextCompat.getDrawable(context, iconId)?.apply {
                setBounds(0, 0, minimumWidth.shr(1), minimumHeight.shr(1))
            }
        } else {
            null
        }
        descriptionTextView.compoundDrawablePadding = if (drawable == null) 0 else DensityUtil.dip2px(context, 10f)
        if (drawable != null) descriptionTextView.setCompoundDrawables(null, null, drawable, null)
    }

    fun add2View(view: View,
                 width: Int = LayoutParams.WRAP_CONTENT,
                 height: Int = LayoutParams.WRAP_CONTENT,
                 params: LayoutParams.() -> Unit) {
        view.layoutParams = LayoutParams(width, height).apply {
            topToTop = LayoutParams.PARENT_ID
            bottomToBottom = LayoutParams.PARENT_ID
            params(this)
        }

        addView(view, viewIndex)
        viewIndex++
    }

}